from datetime import datetime

from fastapi import APIRouter
from fastapi.params import Depends, Query
from pydantic import Field, BaseModel
from sqlalchemy import text
from sqlalchemy.orm import Session

from commons.BaseResponse import BaseResponse
from commons.PaginationBase import PaginationBase
from datebase import get_db
from utils.EncryptionUtil import oauth2_scheme

mac_report_router = APIRouter(prefix="/report", tags=["用电查询"], dependencies=[Depends(oauth2_scheme)])


class MacSearch(PaginationBase):
    fsiteNo: str = Field(None, description="站点编号")
    date: str = Field(None, description="日期")


@mac_report_router.get("/macreport001", summary="每日用电")
def mac_report_001(mac: MacSearch = Query(), db: Session = Depends(get_db)):
    sql = "select * from proc_rpt_macreport001( :fsiteNo , :date );"
    data = db.execute(text(sql), {
        "fsiteNo": mac.fsiteNo,
        "date": mac.date,
    }).fetchall()
    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fentryid'] = data[key][0]
        d1['fsiteNo'] = data[key][1]
        d1['fsiteName'] = data[key][2]
        d1['fusenum1'] = data[key][3]
        d1['fnumrate1'] = data[key][4]
        d1['fflag1'] = data[key][5]
        d1['fusenum2'] = data[key][6]
        d1['fnumrate2'] = data[key][7]
        d1['fflag2'] = data[key][8]
        d1['fusenum3'] = data[key][9]
        d1['fnumrate3'] = data[key][10]
        d1['fflag3'] = data[key][11]
        data_list.append(d1)
    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@mac_report_router.get("/macreport002", summary="每日用电")
def mac_report_002(mac: MacSearch = Query(), db: Session = Depends(get_db)):
    sql = "select * from proc_rpt_macreport002( :site_no , :date );"
    data = db.execute(text(sql), {
        "site_no": mac.fsiteNo,
        "date": mac.date,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fentryid'] = data[key][0]
        d1['ftime'] = data[key][1]
        d1['fusenum1'] = data[key][2]
        d1['fusenum2'] = data[key][3]
        d1['fusenum3'] = data[key][4]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@mac_report_router.get("/macreport003", summary="每月用电")
def mac_get_mac_report_003(mac: MacSearch = Query(), db: Session = Depends(get_db)):
    sql = "select * from proc_rpt_macreport003( :fsiteNo , :date );"
    data = db.execute(text(sql), {
        "fsiteNo": mac.fsiteNo,
        "date": mac.date,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fentryid'] = data[key][0]
        d1['fsiteNo'] = data[key][1]
        d1['fsiteName'] = data[key][2]
        d1['fusenum1'] = data[key][3]
        d1['fnumrate1'] = data[key][4]
        d1['fflag1'] = data[key][5]
        d1['fusenum2'] = data[key][6]
        d1['fnumrate2'] = data[key][7]
        d1['fflag2'] = data[key][8]
        d1['fusenum3'] = data[key][9]
        d1['fnumrate3'] = data[key][10]
        d1['fflag3'] = data[key][11]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@mac_report_router.get("/macreport004", summary="每月用电")
def mac_get_report_004(mac: MacSearch = Query(), db: Session = Depends(get_db)):
    sql = "select * from proc_rpt_macreport004( :site_no , :date );"
    data = db.execute(text(sql), {
        "site_no": mac.fsiteNo,
        "date": mac.date,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fentryid'] = data[key][0]
        d1['ftime'] = data[key][1]
        d1['fusenum1'] = data[key][2]
        d1['fusenum2'] = data[key][3]
        d1['fusenum3'] = data[key][4]

        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@mac_report_router.get("/macreport005", summary="每年用电")
def mac_get_report_005(mac: MacSearch = Query(), db: Session = Depends(get_db)):
    sql = "select * from proc_rpt_macreport005( :site_no , :date );"
    data = db.execute(text(sql), {
        "site_no": mac.fsiteNo,
        "date": mac.date,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fentryid'] = data[key][0]
        d1['fsiteNo'] = data[key][1]
        d1['fsiteName'] = data[key][2]
        d1['fusenum1'] = data[key][3]
        d1['fnumrate1'] = data[key][4]
        d1['fflag1'] = data[key][5]
        d1['fusenum2'] = data[key][6]
        d1['fnumrate2'] = data[key][7]
        d1['fflag2'] = data[key][8]
        d1['fusenum3'] = data[key][9]
        d1['fnumrate3'] = data[key][10]
        d1['fflag3'] = data[key][11]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@mac_report_router.get("/macreport006", summary="每年用电")
def mac_get_report_006(mac: MacSearch = Query(), db: Session = Depends(get_db)):
    sql = "select * from proc_rpt_macreport006( :site_no , :date );"
    data = db.execute(text(sql), {
        "site_no": mac.fsiteNo,
        "date": mac.date,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fentryid'] = data[key][0]
        d1['ftime'] = data[key][1]
        d1['fusenum1'] = data[key][2]
        d1['fusenum2'] = data[key][3]
        d1['fusenum3'] = data[key][4]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


class MacSearch(BaseModel):
    fno: str = Field(None, description="客户编号")
    date: str = Field(None, description="日期")


@mac_report_router.get("/appmacreport001", summary="每日用电 app调用")
def mac_get_report_001(mac: MacSearch, db: Session = Depends(get_db)):
    sql = "select * from proc_rpt_macreport001( :site_no , :date );"
    data = db.execute(text(sql), {
        "site_no": mac.fno,
        "date": mac.date,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fentryid'] = data[key][0]
        d1['fsiteNo'] = data[key][1]
        d1['fsiteName'] = data[key][2]
        d1['fusenum1'] = data[key][3]
        d1['fnumrate1'] = data[key][4]
        d1['fflag1'] = data[key][5]
        d1['fusenum2'] = data[key][6]
        d1['fnumrate2'] = data[key][7]
        d1['fflag2'] = data[key][8]
        d1['fusenum3'] = data[key][9]
        d1['fnumrate3'] = data[key][10]
        d1['fflag3'] = data[key][11]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)
